﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_RoleAssign : UserControl, IRoleAssignView
{
    #region IRoleAssignView Members

    /// <summary>
    /// 获取或设定被分配角色对象的Id，被分配对象可以是<see cref="User"/>,<see cref="UserGroup"/>,<see cref="Org"/>
    /// </summary>
    public string CurrentId
    {
        get { return ViewState["CurrentId"] != null ? ViewState["CurrentId"].ToString() : null; }
        set { ViewState["CurrentId"] = value; }
    }

    /// <summary>
    /// 获取或设定一个值，该数值表示是否被编辑的的对象否是为用户,默认为false
    /// </summary>
    public bool IsEditUser
    {
        get { return CurrentId.Length != 36; }
    }

    /// <summary>
    /// 获取已经选定的角色Id集合
    /// </summary>
    public string[] SelectedRoleIds
    {
        get
        {
            string[] selectedIds = HiddenBoxAssignRoleId.Value.Split(',');
            var result = new string[selectedIds.Length];
            int index = 0;
            foreach (string item in selectedIds)
            {
                result[index] = item;
                index++;
            }
            return result;
        }
    }

    public void BindNotAssignRole(IEnumerable<Role> allRoles)
    {
        lbNoAssignRole.Items.Clear();
        lbNoAssignRole.DataSource = allRoles;
        lbNoAssignRole.DataBind();
        if (lbNoAssignRole.Items.Count != 0)
            lbNoAssignRole.SelectedIndex = 0;
    }

    public void BindExistRole(IEnumerable<Role> roles)
    {
        lbAssignRole.Items.Clear();
        lbAssignRole.DataSource = roles;
        lbAssignRole.DataBind();
        if (lbAssignRole.Items.Count != 0)
            lbAssignRole.SelectedIndex = 0;
    }

    public void AssignRoleTo(User user)
    {
        Presenter.AssignRoleTo(user);
    }


    public RoleAssignPresenter Presenter { get; set; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="member">usergroup or role</param>
    /// <exception cref="ArgumentNullException">member is null</exception>
    public void AssignRoleTo(Member member)
    {
        if (member == null)
            throw new ArgumentNullException("member");
        Presenter.AssignRoleTo(member);
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new RoleAssignPresenter(this);
        if (!IsPostBack)
        {
            Presenter.InitilizeView();
        }
        Presenter.ViewLoad();
    }
}